import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:speed_random/app_styles.dart';
import 'package:speed_random/common/common_fuc.dart';
import 'package:speed_random/global_data.dart';
import 'package:share_plus/share_plus.dart';

class itemHistoryCard2Widget extends StatelessWidget {
  final Map<String, dynamic> numbers;
  final Color textColor;
  final Color bgColor;
  final bool isRandColor;

  const itemHistoryCard2Widget({
    Key? key,
    required this.numbers,
    this.textColor = AppStyles.colorPageText,
    this.bgColor = AppStyles.colorPagebg,
    this.isRandColor =false,
  }) : super(key: key);




  @override
  Widget build(BuildContext context) {
    var zonx = "总项".tr+"：${numbers["list"].split(",").length}";
    var sumx = "总和".tr+"：${numbers["sum"]}";
    return Card(
      elevation: 3, // 设置卡片阴影
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12), // 设置卡片圆角
        side: BorderSide(
          color: Colors.grey.withOpacity(0.3), // 设置边框颜色
          width: 1, // 设置边框宽度
        ),
      ),
      color: bgColor, // 设置卡片背景颜色
     // margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 16), // 设置卡片外边距
      child: ListTile(
        title: Row(
          children: [
            Expanded(
              child: Text(
                numbers["time"],
                style:  TextStyle(
                  color: textColor,
                  fontSize: 12,

                ),
              ),
            ),
            IconButton(
              icon:  Icon(Icons.copy,color: textColor,),
              onPressed: () {

                var copytext = numbers["list"];
                if(numbers["isshowsum"]){
                  copytext = copytext+"\n"+zonx+"    "+sumx;
                }

                // 实现复制随机数到剪贴板的功能
                Clipboard.setData(ClipboardData(text: copytext));
                ScaffoldMessenger.of(context).showSnackBar(
                   SnackBar(content: Text("已复制到剪贴板".tr)),
                );
              },
            ),
            IconButton(
              icon:  Icon(Icons.share,color: textColor,),
              onPressed: () async {
                var shareText;
                if (numbers["isshowsum"]) {
                  shareText = '${numbers["list"]}\n$zonx    $sumx';
                } else {
                  shareText = '${numbers["list"]}';
                }
                shareText=globalData.appName+"\n\n"+shareText+"\n\n"+"App下载地址".tr+"\n${globalData.appInfo.ch?.updateUrl}";
                final params = ShareParams(
                    title:globalData.appName ,
                    //subject: "Random Number",
                    //uri: Uri.parse(GlobalData.appUrl),
                    text: shareText
                );
                final result = await SharePlus.instance.share(params);
                if (result.status == ShareResultStatus.success) {
                  print('Thank you for sharing my website!');
                }
              },
            ),
          ],
        ),
        subtitle: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            // ... 已有代码 ...

            isRandColor?
            Wrap(
              spacing: 5.0,
              runSpacing: 5.0,
              children: numbers["list"].split(",").map<Widget>((number) { // 明确指定map返回Widget类型
                return Container(
                  padding: EdgeInsets.all(5),
                  decoration: BoxDecoration(
                    color: commonFuc.hexToColor(number),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Text(
                    number.trim(),
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                    ),
                  ),
                );
              }).toList(),
            )
:
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                numbers["list"].length>20?
                Expanded(child: Text(
                  numbers["list"],
                  style:  TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                    color: textColor,
                  ),
                ))
                    : Text(
                  numbers["list"],
                  style:  TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                    color: textColor,
                  ),
                ),
              ],
            ),
            const SizedBox(height: 10),
           if(numbers["isshowsum"])
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                 Text(
                   zonx,
                  style:  TextStyle(
                    fontSize: 14,
                    color: textColor,
                  ),
                ),
                SizedBox(width: 10),
                Text(
                  sumx,
                  style:  TextStyle(
                    fontSize: 14,
                    color: textColor,
                  ),
                ),
              ],
            )
          ],
        ),
        isThreeLine: true,
        //contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), // 设置内容内边距
      ),
    );
  }
}
